package com.lanou.wuyou.gift.category;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.base.BaseFragment;
import com.lanou.wuyou.gift.bean.CategoryProductBean;
import com.lanou.wuyou.gift.tools.NetResultInterface;
import com.lanou.wuyou.gift.tools.NetWorkInstance;

/**
 * Created by dllo on 17/3/18.
 */

public class CategoryProductFragment extends BaseFragment{
    private ListView listViewL,listViewR;
    private int selectIndex;
    public static CategoryProductFragment newInstance(String url){
        Bundle args = new Bundle();
        args.putString("url",url);
        CategoryProductFragment fragment = new CategoryProductFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int setLayout() {
        return R.layout.item_category_product;
    }

    @Override
    public void initView(View view) {
        listViewL = (ListView) view.findViewById(R.id.left_list_view);
        listViewR = (ListView) view.findViewById(R.id.right_list_view);
    }
    @Override
    public void initData() {
        final CategoryProductLeftAdapter adapterL = new CategoryProductLeftAdapter(context);
        listViewL.setAdapter(adapterL);
        final CategoryProductRightAdapter adapterR = new CategoryProductRightAdapter(context);
        listViewR.setAdapter(adapterR);

        String urlStr = getArguments().getString("url");

        NetWorkInstance.getInstance().netRequest(urlStr, new NetResultInterface() {
            @Override
            public void success(String str) {
                Gson gson = new Gson();
                CategoryProductBean bean = gson.fromJson(str,CategoryProductBean.class);
                adapterL.setBean(bean);
                adapterR.setBean(bean);
            }

            @Override
            public void fail(String str) {

            }
        });

        listViewL.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                selectIndex = position;
                adapterL.setSelectIndex(selectIndex);
                listViewL.smoothScrollToPositionFromTop(position - 1,0);
                adapterR.setSelectIndex(selectIndex);
                listViewR.setSelection(selectIndex);
            }
        });
        listViewR.setOnScrollListener(new AbsListView.OnScrollListener() {
            int scrollState;
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                this.scrollState = scrollState;
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                if (this.scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE ){
                    return;
                }
                listViewL.smoothScrollToPositionFromTop(firstVisibleItem - 1 ,0);
                adapterL.setSelectIndex(firstVisibleItem);
            }
        });
    }
}
